﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Base;
using System.Data.Common;


namespace WindowsFormsApplication1
{
    public partial class frmDTNhapdiemBosung : Form
    {
        SqlConnection connection = new SqlConnection("Data Source = .; Initial Catalog = SM; Integrated Security = True");
        string sql_DiemBS;
        string masv;
        DataSet ds_DiemBS;

        public frmDTNhapdiemBosung()
        {
            InitializeComponent();
            DataSet ds = Filldataset(" select MalopID, Tenlop from T03_Lop_hoc");
            lku_Chonlop.Properties.DataSource = ds.Tables[0];
        }

        DataSet Filldataset(string sql)
        {
            DataSet ds = new DataSet();
            try
            {
                SqlDataAdapter adap = new SqlDataAdapter(sql, connection);
                adap.Fill(ds);
                adap.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex.Message);
            }
            return ds;
        }

        public void get_gridviewSV(string malop)
        {
            DataSet ds = Filldataset("exec PC_NDBS_ChonSV N'" + malop + "'");
            gridControl1.DataSource = ds.Tables[0];
        }

        private void lku_Chonlop_EditValueChanged(object sender, EventArgs e)
        {
            string malop = lku_Chonlop.EditValue.ToString();
            get_gridviewSV(malop);
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            masv = gridView1.GetRowCellValue(e.RowHandle, gcol_masv).ToString();
            sql_DiemBS = "exec PC_NDBosung N'" + masv + "'";            
            ds_DiemBS = Filldataset(sql_DiemBS);
            gridControl2.DataSource = ds_DiemBS.Tables[0];
        }

        public void query(string command)
        {
            connection.Open();
            SqlCommand sqlCommand = connection.CreateCommand();
            sqlCommand.CommandText = command;
            SqlDataReader dr = sqlCommand.ExecuteReader();
            connection.Close();
        }

        private void gridView2_CellValueChanged(object sender, CellValueChangedEventArgs e)
        {
            string d1 = gridView2.GetRowCellValue(e.RowHandle, gCol_DiemL1).ToString();
            string d2 = gridView2.GetRowCellValue(e.RowHandle, gCol_DiemL2).ToString();
            string d3 = gridView2.GetRowCellValue(e.RowHandle, gCol_DiemL3).ToString();
            string id = gridView2.GetRowCellValue(e.RowHandle, gcol_ID).ToString();
            string str = "exec PC_update_NDBS '" + id + "','" + d1 + "','" + d2 + "','" + d3 + "'";
            query(str);
        }

      
    }
}
